// DlgPreviewHTML.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgPreviewHTML.h"
#include "afxdialogex.h"
#include "sendmessage.h"

#define TIMER_ID 1001

// CDlgPreviewHTML dialog

IMPLEMENT_DYNAMIC(CDlgPreviewHTML, CDialogEx)

CDlgPreviewHTML::CDlgPreviewHTML(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgPreviewHTML::IDD, pParent)
	, m_szSubject(_T(""))
	, m_szHTML(_T(""))
{
	m_bSend = TRUE;
	m_bPrint = FALSE;
	m_bPrintPreview = FALSE;
	m_bPromptPrinter = FALSE;
}

CDlgPreviewHTML::~CDlgPreviewHTML()
{
}

void CDlgPreviewHTML::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_SUBJECT, m_szSubject);
	DDX_Control(pDX, IDC_BUTTON_BOLD, m_btnBold);
	DDX_Control(pDX, IDC_BUTTON_ITALIC, m_btnItalic);
	DDX_Control(pDX, IDC_BUTTON_COLOR, m_btnColor);
	DDX_Control(pDX, IDC_BUTTON_UNDO, m_btnUndo);
	DDX_Control(pDX, IDC_BUTTON_UNLINK, m_btnUnlink);
}


BEGIN_MESSAGE_MAP(CDlgPreviewHTML, CDialogEx)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUTTON_BOLD, &CDlgPreviewHTML::OnBnClickedButtonBold)
	ON_BN_CLICKED(IDC_BUTTON_ITALIC, &CDlgPreviewHTML::OnBnClickedButtonItalic)
	ON_BN_CLICKED(IDC_BUTTON_COLOR, &CDlgPreviewHTML::OnBnClickedButtonColor)
	ON_BN_CLICKED(IDC_BUTTON_UNDO, &CDlgPreviewHTML::OnBnClickedButtonUndo)
	ON_BN_CLICKED(IDC_BUTTON_UNLINK, &CDlgPreviewHTML::OnBnClickedButtonUnlink)
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()


// CDlgPreviewHTML message handlers


BOOL CDlgPreviewHTML::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//set send button
	if( m_bSend )
	{
		GetDlgItem( IDOK )->SetWindowText( _T("Send") );
	}
	else
	{
		GetDlgItem( IDC_STATIC_SUBJECT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_SUBJECT )->ShowWindow( SW_HIDE );		
	}

	// create control
	if( !m_ctrlEditHTML.CreateFromStatic( IDC_STATIC_HTML_EDIT, this ))
	{
		ASSERT( FALSE );
		EndDialog( IDCANCEL );
		return FALSE;
	}

	// wait for edit control to init
	m_ctrlEditHTML.WaitForComplete();

	// set message
	m_ctrlEditHTML.SetDocumentHTML( m_szHTML, TRUE );

	// check for print support
	if( m_bPrint && !m_bPrintPreview )
	{
		// set margins
		appConfig.SetMargins();
		// print document
		m_ctrlEditHTML.PrintDocument( m_bPromptPrinter != 0 );
		// reset margins
		appConfig.SetMargins( TRUE );

		// end dialog
		EndDialog( IDOK );
		return FALSE;
	}

	// set buttons
	HICON hIcon = NULL;
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_BOLD), IMAGE_ICON, 16, 16, 0);
	m_btnBold.SetIcon( hIcon );
	m_btnBold.SetTooltip(_T("Bold highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_ITALIC), IMAGE_ICON, 16, 16, 0);
	m_btnItalic.SetIcon( hIcon );
	m_btnItalic.SetTooltip(_T("Italicize highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_COLOR), IMAGE_ICON, 16, 16, 0);
	m_btnColor.SetIcon( hIcon );
	m_btnColor.SetTooltip(_T("Set color for highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_UNDO), IMAGE_ICON, 16, 16, 0);
	m_btnUndo.SetIcon( hIcon );
	m_btnUndo.SetTooltip(_T("Undo last"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_UNLINK), IMAGE_ICON, 16, 16, 0);
	m_btnUnlink.SetIcon( hIcon );
	m_btnUnlink.SetTooltip(_T("Unlink highlighted text"));

	// set button for print
	if( m_bPrint )
		GetDlgItem( IDOK )->SetWindowText( _T("Print") );

	// size the dialog
	SizeDialog();

	// set focus
	m_ctrlEditHTML.ExecCommand( IDM_CARETBROWSINGMODE, OLECMDEXECOPT_DODEFAULT );
	m_ctrlEditHTML.SetFocus();


	return FALSE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgPreviewHTML::OnOK()
{
	// update data
	UpdateData();

	// wait for new document to load
	m_ctrlEditHTML.WaitForComplete();

	// get HTML
	m_ctrlEditHTML.GetDocumentHTML( m_szHTML );

	// check if printing
	if( m_bPrint )
	{
		// set margins
		appConfig.SetMargins();
		// print document
		m_ctrlEditHTML.PrintDocument( m_bPromptPrinter != 0 );
		// reset margins
		appConfig.SetMargins( TRUE );
	}

	CDialogEx::OnOK();
}


void CDlgPreviewHTML::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialogEx::OnCancel();
}


void CDlgPreviewHTML::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// size the dialog
	SizeDialog();
}


void CDlgPreviewHTML::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	// set min info
	if( m_ctrlEditHTML.GetSafeHwnd() )
	{
		lpMMI->ptMinTrackSize.x = 600;
		lpMMI->ptMinTrackSize.y = 400;
	}

	CDialogEx::OnGetMinMaxInfo(lpMMI);
}


void CDlgPreviewHTML::OnBnClickedButtonBold()
{
	// set bold
	m_ctrlEditHTML.Bold();
	m_ctrlEditHTML.SetFocus();
}


void CDlgPreviewHTML::OnBnClickedButtonItalic()
{
	// set italic
	m_ctrlEditHTML.Italic();
	m_ctrlEditHTML.SetFocus();
}


void CDlgPreviewHTML::OnBnClickedButtonColor()
{
	// get color and set it
	CColorDialog dlg;
	if( dlg.DoModal() == IDOK )
	{
		COLORREF cColor = dlg.GetColor();
		m_ctrlEditHTML.SetForeColor( cColor );
		m_ctrlEditHTML.SetFocus();
	}
}


void CDlgPreviewHTML::OnBnClickedButtonUndo()
{
	// set undo
	m_ctrlEditHTML.ExecCommand( IDM_UNDO, OLECMDEXECOPT_DODEFAULT );
	m_ctrlEditHTML.SetFocus();
}


void CDlgPreviewHTML::OnBnClickedButtonUnlink()
{
	// set undo
	m_ctrlEditHTML.ExecCommand( IDM_UNLINK, OLECMDEXECOPT_DODEFAULT );
	m_ctrlEditHTML.SetFocus();
}


BOOL CDlgPreviewHTML::SizeDialog(void)
{
	// check if we are initialized yet
	if( !m_ctrlEditHTML )
		return FALSE;

	CWnd *pWindow = NULL;
	CRect rectDlg;
	CRect rectCtl;

	GetWindowRect( rectDlg );
	INT nWidth = rectDlg.Width();
	INT nHeight = rectDlg.Height();

	// IDC_EDIT_SUBJECT
	pWindow = GetDlgItem( IDC_EDIT_SUBJECT );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectCtl.top, rectDlg.Width() - 80, rectCtl.Height(), FALSE );
	}

	// IDC_EXPLORER
	if( m_bSend )
	{
		m_ctrlEditHTML.GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		m_ctrlEditHTML.MoveWindow( rectCtl.left, rectCtl.top, rectDlg.Width() - 35, rectDlg.Height() - 135, FALSE );
	}
	else
	{
		m_ctrlEditHTML.GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		m_ctrlEditHTML.MoveWindow( rectCtl.left, rectCtl.top - 20, rectDlg.Width() - 35, rectDlg.Height() - 155, FALSE );
	}

	// IDOK
	pWindow = GetDlgItem( IDOK );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectDlg.Width() - 107, rectDlg.Height() - 86, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDCANCEL
	pWindow = GetDlgItem( IDCANCEL );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectDlg.Width() - 192, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDC_BUTTON_BOLD
	pWindow = GetDlgItem( IDC_BUTTON_BOLD );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDC_BUTTON_ITALIC
	pWindow = GetDlgItem( IDC_BUTTON_ITALIC );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDC_BUTTON_COLOR
	pWindow = GetDlgItem( IDC_BUTTON_COLOR );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDC_BUTTON_UNDO
	pWindow = GetDlgItem( IDC_BUTTON_UNDO );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// IDC_BUTTON_UNLINK
	pWindow = GetDlgItem( IDC_BUTTON_UNLINK );
	if( pWindow != NULL )
	{
		pWindow->GetWindowRect( rectCtl );
		ScreenToClient( rectCtl );
		pWindow->MoveWindow( rectCtl.left, rectDlg.Height() - 70, rectCtl.Width(), rectCtl.Height(), FALSE );
	}

	// redraw window
	Invalidate();

	return TRUE;
}


BOOL CDlgPreviewHTML::SetSelection(CString szText)
{
	BOOL bFound = FALSE;
	CComBSTR bsText(szText.GetLength()+1,(LPCTSTR)szText);
	CComPtr<IHTMLDocument2> pDocument;

	m_ctrlEditHTML.GetDocument( &pDocument );
	if( pDocument )
	{
		CComPtr<IHTMLElement> pElement;
		pDocument->get_body( &pElement );
		if( pElement )
		{
			CComPtr<IHTMLBodyElement> pBody;
			pElement->QueryInterface(IID_IHTMLBodyElement,(void**)&pBody);
			if( pBody )
			{
				CComPtr<IHTMLTxtRange> pRange;
				pBody->createTextRange( &pRange );
				if( pRange )
				{
					pRange->findText((BSTR)bsText, 0, 6, (VARIANT_BOOL*)&bFound);
					if( bFound > 0 )
					{
						pRange->select();
					}
				}
			}
		}
	}

	return bFound;
}


void CDlgPreviewHTML::EnableButtons(BOOL bEnable)
{
	// enable/disable window
	if( bEnable )
	{
		GetDlgItem(IDOK)->EnableWindow();
		GetDlgItem(IDC_BUTTON_BOLD)->EnableWindow();
		GetDlgItem(IDC_BUTTON_ITALIC)->EnableWindow();
		GetDlgItem(IDC_BUTTON_COLOR)->EnableWindow();
		GetDlgItem(IDC_BUTTON_UNDO)->EnableWindow();
		GetDlgItem(IDC_BUTTON_UNLINK)->EnableWindow();
		
		m_ctrlEditHTML.EnableWindow();
	}
	else
	{
		GetDlgItem(IDOK)->EnableWindow( FALSE );
		GetDlgItem(IDC_BUTTON_BOLD)->EnableWindow( FALSE );
		GetDlgItem(IDC_BUTTON_ITALIC)->EnableWindow( FALSE );
		GetDlgItem(IDC_BUTTON_COLOR)->EnableWindow( FALSE );
		GetDlgItem(IDC_BUTTON_UNDO)->EnableWindow( FALSE );
		GetDlgItem(IDC_BUTTON_UNLINK)->EnableWindow( FALSE );

		m_ctrlEditHTML.EnableWindow( FALSE );
	}
}
